package com.shroo8.footballblog.controllers.api;

import java.util.Locale;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.shroo8.footballblog.bo.api.UpdateArticleBo;

@Controller
public class UpdateArticleController {
	private static final Logger logger = LoggerFactory.getLogger(UpdateArticleController.class);

	@Resource(name="updateArticleBo", type=com.shroo8.footballblog.bo.api.UpdateArticleBoImpl.class)
	private UpdateArticleBo updateArticleBo;
	
	/*
	 * using @RequestBody as a work around as spring does not support PUT with @RequestParam
	 * */
	@RequestMapping(value = "/articles/{articleId}", method = RequestMethod.PUT)
	public @ResponseBody String updateArticle(Locale locale, @PathVariable("articleId") String articleId,@RequestBody MultiValueMap<String,String> body) {
		logger.info("Welcome to putArticle! The client locale is {}.", locale);
		logger.info("article id is:" + articleId);

		String articleTitle = body.getFirst("articleTitle");
		String articleText = body.getFirst("articleText");
		logger.info("article title is:" + articleTitle);
		logger.info("article text is:" + articleText);
		
		updateArticleBo.updateArticle(articleId, articleTitle, articleText);
		return "";
	}

}
